<!-- 首页 -->
<script setup lang="ts">
	import { ref, computed, onMounted } from 'vue';
	// 导入 onShow 生命周期钩子
	import { onShow, onLoad } from '@dcloudio/uni-app'
	// 导入登录接口
	import { activityListApi, joinActivityApi, cancelActivityApi, activityDetailApi, activitySearchApi } from '../../http/home.js';

	// 活动列表分页参数
	const pageNum = 1;
	const pageSize = 10;
	// 活动列表数据
	const activityList = ref([]);


	let app = getApp();

	onMounted(() => {
		// 获取活动列表逻辑
		activityListApi(pageNum, pageSize).then(data => {
			activityList.value = data.records;
			// activityList.value.forEach((item : any) => {
			// 	item.isJoin = true;
			// })
			console.log(activityList.value);
		})
		app.$ws.on('new_activity', (msg) => {
			// 接收活动日志
			uni.showToast({
				title: msg.title
			})
		})
	})

	// 时间格式化（简化版）
	const formatTime = (timeStr : string) => {
		if (!timeStr) return '';
		return new Date(timeStr).toLocaleString();
	};

	// 控制报名与取消报名状态
	// let isTrue = true

	// 在页面显示时检查是否已登录，未登录则跳转到登录页面
	onShow(() => {
		// 判断是否已登录
		const checkLogin = () => {
			// 从本地存储中获取 accessToken
			const token = uni.getStorageSync('accessToken')
			if (!token) {
				// 如果未登录，跳转到登录页
				uni.navigateTo({
					url: '/pages/login/login' // 登录页面路径
				})
			}
		}
		// 获取活动列表逻辑
		activityListApi(pageNum, pageSize).then(data => {
			activityList.value = data.records;
			// activityList.value.forEach((item : any) => {
			// 	item.isJoin = true;
			// })
			console.log(activityList.value);
		})
	})

	// 新增：活动状态文本
	const getStatusText = (status : number) => {
		switch (status) {
			case 0: return '未开始';
			case 1: return '进行中';
			case 2: return '已结束';
			default: return '';
		}
	};

	// 搜索框内容
	const searchValue = ref('');
	// 根据活动名查询活动
	const search = async (searchValue) => {
		// console.log(searchValue);
		await activitySearchApi(pageNum, pageSize, searchValue).then(data => {
			activityList.value = [];
			activityList.value = data.records;
			// console.log(activityList.value);
		})

	}

	// 分类导航数据
	const categoryList = ref([
		{ icon: '/static/images/lecture.png', text: '讲座' },
		{ icon: '/static/images/volunteer.png', text: '志愿' },
		{ icon: '/static/images/sports.png', text: '运动' },
		{ icon: '/static/images/other.png', text: '其他' },
	]);

	// 分类点击事件
	const handleCategoryTap = (item : any) => {
		// console.log('点击分类：', item.text);
		// 跳转到分类页面
		uni.navigateTo({
			url: `./category?type=${item.text}`
		});
	};

	// 跳转到活动详情页
	const goToDetail = (activityId : number) => {
		uni.navigateTo({
			url: `/pages/detail/detail?id=${activityId}`
		});
	};

	// 报名按钮点击事件
	const handleAction = (activity : any) => {
		// console.log(activity.privateStatus);
		joinActivityApi(activity.id).then(data => {
			// 更新该活动的报名状态为已报名（1）
			activity.privateStatus = 1;

			// activity.isJoin = false
			uni.showToast({
				title: '报名成功',
				icon: 'success'
			})

			// history.go(0)

		}).catch(err => {
			console.log(err);
		})
	};

	// 取消报名按钮点击事件
	const cancelAction = (activity : any) => {
		cancelActivityApi(activity.id).then(data => {
			// 更新该活动的报名状态为未报名（0）
			activity.privateStatus = 0;

			// activity.isJoin = true
			uni.showToast({
				title: '取消报名成功',
				icon: 'success'
			})

			// history.go(0)

		}).catch(err => {
			console.log(err);
		})
	};
</script>

<template>
	<view class="home-page">
		<!-- 搜索栏 -->
		<view class="search-bar">
			<input class="search-input" type="text" placeholder="请输入关键词/活动名称" placeholder-style="color: #999"
				v-model="searchValue" />
			<image class="search-icon" src="/static/images/search.png" mode="widthFix"></image>
			<button @tap="search(searchValue)">搜索</button>
		</view>

		<!-- 分类导航 -->
		<view class="category-nav">
			<view class="category-item" v-for="(item, index) in categoryList" :key="index"
				@tap="handleCategoryTap(item)">
				<image class="category-icon" :src="item.icon" mode="widthFix"></image>
				<text class="category-text">{{ item.text }}</text>
			</view>
		</view>

		<!-- 活动推荐标题 -->
		<text class="recommend-title">活动推荐</text>

		<!-- 活动列表 -->
		<view class="activity-list">
			<view class="activity-item" v-for="(activity, index) in activityList" :key="index">
				<view @tap="goToDetail(activity.id)">
					<image class="activity-img" :src="activity.poster" mode="widthFix"></image>
					<view class="activity-info">
						<!-- 新增：显示活动状态 -->
						<view class="activity-status">{{ getStatusText(activity.activityStatus) }}</view>
						<view class="activity-title">{{ activity.title }}</view>
						<view class="activity-detail">活动描述：{{ activity.description }}</view>
						<!-- 使用格式化后的时间 -->
						<view class="activity-time">
							<view class="time-item">
								<image class="time-icon" src="/static/images/calendar.png" mode="widthFix"></image>
								<text>开始：{{ formatTime(activity.startTime) }}</text>
							</view>
							<view class="time-item">
								<image class="time-icon" src="/static/images/calendar.png" mode="widthFix"></image>
								<text>结束：{{ formatTime(activity.endTime) }}</text>
							</view>
						</view>
						<view class="activity-point">
							<image class="point-icon" src="/static/images/point.png" mode="widthFix"></image>
							<text>积分数：{{ activity.point }}</text>
						</view>
					</view>
				</view>
				<view class="button-group">
					<button class="action-btn" style="backgroundColor: skyblue" @tap="handleAction(activity)"
						v-if="activity.privateStatus == 0 || activity.privateStatus == null">
						报名
					</button>
					<button class="action-btn" style="backgroundColor: #FF8888" @tap="cancelAction(activity)" v-else>
						取消报名
					</button>

				</view>
			</view>
		</view>
	</view>
</template>

<style scoped lang="scss">
	.home-page {
		background-color: #f8f9fa;
		min-height: 100vh;
		padding-bottom: 100rpx;
		font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
	}

	/* 搜索栏 - 添加阴影和圆角 */
	.search-bar {
		display: flex;
		align-items: center;
		background-color: #ffffff;
		padding: 20rpx 30rpx;
		margin: 20rpx;
		border-radius: 50rpx;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.05);
		position: relative;

		.search-input {
			flex: 1;
			height: 70rpx;
			font-size: 28rpx;
			padding-left: 60rpx;
		}

		.search-icon {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			left: 50rpx;
		}
	}

	/* 分类导航 - 添加悬停效果和间距 */
	.category-nav {
		display: flex;
		justify-content: space-around;
		padding: 30rpx 0;
		margin: 20rpx;
		background-color: #ffffff;
		border-radius: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);

		.category-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			transition: transform 0.2s ease;

			&:active {
				transform: scale(0.95);
			}

			.category-icon {
				width: 80rpx;
				height: 80rpx;
				margin-bottom: 15rpx;
			}

			.category-text {
				font-size: 26rpx;
				color: #555;
				font-weight: 500;
			}
		}
	}

	/* 活动推荐标题 - 添加装饰线 */
	.recommend-title {
		display: block;
		font-size: 36rpx;
		font-weight: 700;
		margin: 40rpx 30rpx 20rpx;
		color: #333;
		position: relative;
		padding-left: 20rpx;

		&::before {
			content: "";
			position: absolute;
			left: 0;
			top: 50%;
			transform: translateY(-50%);
			width: 8rpx;
			height: 36rpx;
			background: linear-gradient(to bottom, #4e9eff, #007aff);
			border-radius: 8rpx;
		}
	}

	/* 活动列表 - 卡片化设计 */
	.activity-list {
		padding: 0 20rpx;
		margin-bottom: 40rpx;

		.activity-item {
			background-color: #ffffff;
			border-radius: 24rpx;
			overflow: hidden;
			margin-bottom: 30rpx;
			box-shadow: 0 6rpx 24rpx rgba(0, 0, 0, 0.06);
			transition: transform 0.3s ease, box-shadow 0.3s ease;

			&:active {
				transform: translateY(-4rpx);
				box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
			}

			.activity-img {
				width: 100%;
				height: 300rpx;
				display: block;
				object-fit: cover;
			}

			.activity-info {
				padding: 30rpx;

				.activity-title {
					font-size: 32rpx;
					font-weight: 700;
					margin-bottom: 15rpx;
					color: #333;
					line-height: 1.4;
				}

				.activity-status {
					background-color: #4e9eff;
					color: white;
					font-size: 24rpx;
					padding: 6rpx 16rpx;
					border-radius: 16rpx;
					display: inline-block;
					margin-bottom: 10rpx;
				}

				.activity-detail {
					font-size: 26rpx;
					color: #666;
					margin-bottom: 10rpx;
					line-height: 1.5;
				}

				/* 新增：时间区域样式 */
				.activity-time {
					background-color: #f8f9ff;
					border-radius: 12rpx;
					padding: 16rpx 20rpx;
					margin: 20rpx 0;
					border-left: 4rpx solid #4e9eff;

					.time-item {
						display: flex;
						align-items: center;
						font-size: 26rpx;
						color: #555;
						margin-bottom: 8rpx;

						&:last-child {
							margin-bottom: 0;
						}

						.time-icon {
							width: 30rpx;
							height: 30rpx;
							margin-right: 10rpx;
						}
					}
				}

				/* 新增：积分区域样式 */
				.activity-point {
					display: flex;
					align-items: center;
					font-size: 26rpx;
					color: #ff8c00;
					background-color: #fff9f0;
					border-radius: 12rpx;
					padding: 12rpx 16rpx;
					margin-bottom: 10rpx;

					.point-icon {
						width: 32rpx;
						height: 32rpx;
						margin-right: 8rpx;
					}
				}

				/* 按钮组样式 */
				.button-group {
					display: flex;
					justify-content: space-between;
					margin-top: 30rpx;

					.action-btn {
						flex: 1;
						margin: 0 10rpx;
						padding: 18rpx 0;
						font-size: 28rpx;
						border-radius: 50rpx;
						color: white;
						border: none;
						transition: all 0.2s ease;

						&:first-child {
							background: linear-gradient(45deg, #4e9eff, #007aff);
						}

						&:last-child {
							background: linear-gradient(45deg, #ff7e5f, #ff5252);
						}

						&:active {
							transform: scale(0.96);
						}
					}
				}
			}
		}
	}

	/* 响应式调整 */
	@media (min-width: 768px) {
		.home-page {
			max-width: 750px;
			margin: 0 auto;
			padding: 0 20rpx;
		}

		.activity-list {
			display: grid;
			grid-template-columns: repeat(2, 1fr);
			gap: 30rpx;
		}
	}
</style>